import { ref } from 'vue'
import { getOneLevelCategoryAPI } from '@/api'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'
// 分类封装
function useCategory() {
  const categoryList = ref([])
  const route = useRoute()
  // 设置id默认值
  async function getOneLevelCategory(id = route.params.id) {
    const {
      result: { children }
    } = await getOneLevelCategoryAPI(id)
    categoryList.value = children
    console.log(children)
  }
  getOneLevelCategory()
  // 路由加载前
  onBeforeRouteUpdate(to => {
    getOneLevelCategory(to.params.id)
  })
  return { categoryList }
}
export default useCategory
